package de.jonasjelten.jcalc.mathExps;

import de.jonasjelten.jcalc.exception.JEvaluationException;
import de.jonasjelten.jcalc.math.JNumber;

/**
 *
 * @author michael, jonas
 */
public abstract class JMultiArgMathExp extends JMathExp {

    JMathExp[] args;

    public JMultiArgMathExp(JMathExp[] args) {
        super(args[0].jcalc);
        this.args = args;
    }

    public JNumber[] evalChildren() {
        // start sub threads.
        for (int i = 0; i < args.length; i++) {
            args[i].init();
        }

        // store the results of all sub threads here.
        JNumber[] result = new JNumber[args.length];

        // read results from all subthreads. the result reader will wait until the subthread has finished.
        for (int i = 0; i < args.length; i++) {
            result[i] = args[i].getEvalResult();
        }

        return result;
    }


    public String toString(boolean bracket, String operand) {
        String res = "";
        res = args[0].toString();
        for (int i = 1; i < args.length; i++) {
            res += operand + args[i].toString();
        }

        if (bracket) {
            res = "("+res+")";
        }
        return res;
    }

    @Override
    public int getNonTrivialChildCount() {
        int result = 0;

        for (int i = 0; i < args.length; i++) {
            result += args[i].getNonTrivialMathExpsCount();
        }

        return result;
    }

    public abstract JNumber eval() throws JEvaluationException;

    public abstract String toString();

    public abstract boolean isTrivial();
}
